using CinemachineExtend;
using Services;
using UnityEngine;

[RequireComponent(typeof(CinemathineCore))]
public class CameraController : MonoBehaviour
{
    private Transform transform_player;
    private CinemathineCore cameraCore;
    private EventSystem eventSystem;

    private void Awake()
    {
        cameraCore = GetComponent<CinemathineCore>();
        eventSystem = ServiceLocator.Get<EventSystem>();
    }

    private void Start()
    {
        ActionController actionController = ServiceLocator.Get<ActionController>();
        transform_player = actionController.player.transform;
        SceneInit sceneInit = ServiceLocator.Get<LightController>().SceneInit;
        if (!sceneInit.b_cameraFixed)
            cameraCore.VirtualCamera.Follow = transform_player;
    }

    public void SetSize(float size)
    {
        cameraCore.VirtualCamera.m_Lens.OrthographicSize = size;
        eventSystem.Invoke(EEvent.CameraSizeChange);
    }

    public Rect GetRect(float left = 0, float right = 0, float up = 0, float down = 0)
    {
        float y = Camera.main.orthographicSize;
        float x = y / Screen.height * Screen.width;
        float width = 2 * x + left + right;
        float height = 2 * y + down + up;
        x = -x - left + transform.position.x;
        y = -y - down + transform.position.y;
        return new Rect(x, y, width, height);
    }
}
